import 'package:flutter/material.dart';
import 'package:pos_link/auto_pigeon/poslink_sdk_set.dart';
import 'package:pos_link/model/comm_setting_view_model.dart';
import 'package:pos_link/model/request_db.dart';
import 'package:pos_link/model/response_db.dart';
import 'package:pos_link/model/transaction_view_model.dart';
import 'package:pos_link/pos_link.dart';
import 'package:provider/provider.dart';


void main() {
  runApp(
      MultiProvider(
          providers: [
            ChangeNotifierProvider(create: (context) => TransactionViewModel()),
            ChangeNotifierProvider(create: (context) => CommSettingViewModel()),
            ChangeNotifierProvider(create: (context) => ResponseDataBase()),
            ChangeNotifierProvider(create: (context) => RequestDataBase()),
          ],
          child: const MyApp()
      )
  );
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final PosLink posLink = PosLink();
  var dataMsg = "";
  late TransactionViewModel transactionViewModel;
  late ResponseDataBase responseDataBase;
  late RequestDataBase requestDataBase;
  late CommSettingViewModel commSettingViewModel;
  @override
  void initState() {
    posLink.permissionAllowed();
    posLink.configureFlutterEngine();
    transactionViewModel = context.read<TransactionViewModel>();
    responseDataBase = context.read<ResponseDataBase>();
    requestDataBase = context.read<RequestDataBase>();
    commSettingViewModel = context.read<CommSettingViewModel>();
    super.initState();
  }

  Future<bool> getHandshake() async{
    await POSLinkSetApi().setAIDLSetting();
    return await POSLinkSetApi().handshake();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('POS Link App 插件测试003 打包正式证书'),
        ),
        body: SingleChildScrollView(
          child: Consumer<CommSettingViewModel>(
            builder: (context, viewModel, child) {
              return Center(
                child:Column(
                    children: [
                      Text(dataMsg),
                      Center(
                        child: ElevatedButton(
                          onPressed: () async {
                            await viewModel.setSetting();
                            bool handshakeSuccess = await viewModel.handshake();
                            if (handshakeSuccess) {
                              setState(() {
                                dataMsg = "连接成功";
                              });
                            } else {
                              setState(() {
                                dataMsg = "连接失败";
                              });
                            }
                          },
                          child: const Text("连接AIDL"),
                        ),
                      ),
                      Center(
                        child: ElevatedButton(
                          onPressed: () async {
                            await transactionViewModel.start(
                                responseDataBase, requestDataBase.transactionData!);

                            setState(() {
                              List<Map> a = responseDataBase.transactionData;
                              dataMsg = a[0]["kValue"] + "   " + a[1]["kValue"];
                            });
                          },
                          child: const Text("支付100"),
                        ),
                      ),
                    ]
                ),
              );

            },
          ),
        ),
      ),
    );
  }
}
